<?php

require_once 'q/mvc/action.php';
require_once 'dao/admin/crawler.php';
require_once 'admin/session.php';

class action_crawler extends q_mvc_action {

    public function init(){
         
        $uid = admin_session::require_role('editor');

        $this->view->uid = $uid;
    }

    public function indexAction(){
        echo 'bad access';
        exit;

    }

    public function detailAction(){
        $table = q_request::get('table');
        $id = q_request::get('id');

        $dao = new dao_admin_crawler($table);
        $row = $dao->get($id);


        $this->view->row = $row;
        $this->view->id = $id;
        $this->view->table = $table;
    }

    public function listAction(){
        $table = q_request::get('table');
        $page  = q_request::get('page', 'number', 1);
        $limit = 30;
        $offset = $limit * ( $page - 1 );

        $dao = new dao_admin_crawler($table);
        $list = $dao->get_list($offset, $limit);
        $list_count = $dao->get_list_total();
        $total_page = ceil( $list_count / $limit );
        
        $this->view->page = $page;
        $this->view->total_page = $total_page;
        $this->view->list = $list;
        $this->view->table = $table;
    }

    public function imageAction(){
        $table = q_request::get('table');
        $id = q_request::get('id');

        if(!in_array($table, array('7k7k','4399'))){
            exit();
        }
        if(empty($id)){
            exit();
        }


        $file = dao_admin_crawler::get_image_file($table, $id);

        $rs = getimagesize($file);
        if(!$rs){
            exit;
        }

        header('Content-Type: ' . $rs['mime']);
        readfile($file);
        exit;
        
    }

    public function flashAction(){
        $table = q_request::get('table');
        $id = q_request::get('id');

        if(!in_array($table, array('7k7k','4399'))){
            exit();
        }
        if(empty($id)){
            exit();
        }


        $file = dao_admin_crawler::get_flash_file($table, $id);

        $rs = getimagesize($file);
        if(!$rs){
            exit;
        }

        header('Content-Type: ' . $rs['mime']);
        readfile($file);
        exit;
        
    }

}

